package com.example.armmvvm.ui.constraint

import android.os.Bundle
import android.view.View
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import com.example.arm.base.BaseActivity
import com.example.arm.ext.dpInt
import com.example.armmvvm.databinding.ActivityAutoSizeInConstraintLayoutBinding
import com.example.armmvvm.ui.constraint.normalfallview.NormalOneStateFallView
import com.example.armmvvm.ui.constraint.scrollfallview.SideBySideScrollFallView
import com.example.armmvvm.ui.constraint.statefallview.OneStateFallView
import com.example.armmvvm.ui.fallview.AnchorFallViewController
import com.example.armmvvm.ui.fallview.BottomFallViewController
import com.google.android.material.bottomsheet.BottomSheetBehavior
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.onFailure
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.launch

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.test
 * @createTime 创建时间 ：2021/8/9
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class ConstraintLayoutTestActivity : BaseActivity<ActivityAutoSizeInConstraintLayoutBinding>() {
    override fun initView(savedInstanceState: Bundle?): ActivityAutoSizeInConstraintLayoutBinding {
        return ActivityAutoSizeInConstraintLayoutBinding.inflate(layoutInflater)
    }

    override fun initData(savedInstanceState: Bundle?) {

        binding.viewContainer2.post {

//            binding.viewContainer2.isVisible = true

            // ONE_STATE
//            setOneState()

            // TWO_STATE
//            setTwoState()

            // TWO_NO_TOP_STATE
//            setTwoNoTopState()

            // THREE_STATE
//            setThreeState()

            // THREE_NO_TOP_STATE
//            setThreeNoTopState()
        }

        val bottomFallViewHelper = AnchorFallViewController.getInstance(binding.viewContainer, this)
        val bottomFallViewHelper3 = BottomFallViewController.getInstance(binding.viewContainer3, this)

        binding.first.setOnClickListener {
            bottomFallViewHelper3.hideBottomView()
            NormalOneStateFallView(this, binding.viewContainer).show()
        }

        binding.second.setOnClickListener {
            bottomFallViewHelper.hideBottomView()
            OneStateFallView(this, binding.viewContainer3).show()
        }

        lifecycleScope.launch {
            binding.third.clickEventFlow()
                .collect {
                    bottomFallViewHelper.hideBottomView()
                    SideBySideScrollFallView(this@ConstraintLayoutTestActivity, binding.viewContainer3).show()
                }
        }
    }

    private fun setThreeNoTopState() {
        val behavior = BottomSheetBehavior.from(binding.viewContainer2)
        val containerViewHeight = 550.dpInt
        binding.scrollView.updateLayoutParams {
            height = containerViewHeight
        }
        binding.blankView.updateLayoutParams {
            height = containerViewHeight
        }
        val setHeight = 250.dpInt
        binding.tvContent.updateLayoutParams {
            height = setHeight
        }
        behavior.peekHeight = setHeight
        behavior.isFitToContents = false
        behavior.expandedOffset = binding.root.height - containerViewHeight
        behavior.halfExpandedRatio = 0.5f
        behavior.isHideable = false
        behavior.state = BottomSheetBehavior.STATE_COLLAPSED
    }

    private fun setThreeState() {
        val behavior = BottomSheetBehavior.from(binding.viewContainer2)
        binding.blankView.updateLayoutParams {
            height = 1550.dpInt
        }
        val setHeight = 250.dpInt
        binding.tvContent.updateLayoutParams {
            height = setHeight
        }
        behavior.peekHeight = setHeight
        behavior.isFitToContents = false
        behavior.expandedOffset = 30.dpInt
        behavior.halfExpandedRatio = 0.8f
        behavior.isHideable = false
        behavior.state = BottomSheetBehavior.STATE_COLLAPSED
    }

    private fun setTwoNoTopState() {
        val behavior = BottomSheetBehavior.from(binding.viewContainer2)
        binding.blankView.updateLayoutParams {
            height = 550.dpInt
        }

        val setHeight = 250.dpInt
        binding.tvContent.updateLayoutParams {
            height = setHeight
        }
        behavior.peekHeight = setHeight
        behavior.isFitToContents = true
        behavior.isHideable = false
        // 无用,只有isFitToContents == false 时expandedOffset才有效
        behavior.expandedOffset = 30.dpInt
        behavior.state = BottomSheetBehavior.STATE_COLLAPSED
    }

    private fun setTwoState() {
        val behavior = BottomSheetBehavior.from(binding.viewContainer2)
        binding.blankView.updateLayoutParams {
            height = 3250.dpInt
        }

        val setHeight = 250.dpInt
        binding.tvContent.updateLayoutParams {
            height = setHeight
        }
        behavior.peekHeight = setHeight
        behavior.isFitToContents = true
        behavior.isHideable = false
        // 无用,只有isFitToContents == false 时expandedOffset才有效
        behavior.expandedOffset = 30.dpInt
        behavior.state = BottomSheetBehavior.STATE_COLLAPSED
    }

    private fun setOneState() {
        val behavior = BottomSheetBehavior.from(binding.viewContainer2)
        val setHeight = 250.dpInt
        binding.scrollView.updateLayoutParams {
            height = setHeight
        }
        binding.tvContent.updateLayoutParams {
            height = setHeight
        }
        behavior.peekHeight = setHeight
        behavior.isFitToContents = true
        behavior.isHideable = false
        behavior.state = BottomSheetBehavior.STATE_COLLAPSED
    }


    fun View.clickEventFlow(): Flow<Unit> {

        var lastClickTime = System.currentTimeMillis()

        return callbackFlow {
            setOnClickListener {
                if (System.currentTimeMillis() - lastClickTime > 500) {
                    lastClickTime = System.currentTimeMillis()
                    trySendBlocking(Unit)
                        .onFailure { throwable ->
                            throwable?.printStackTrace()
                        }
                }
            }
            awaitClose {
                setOnClickListener(null)
            }
        }
    }
}